package com.ruoyi.system.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.system.mapper.MedicalStudentMapper;
import com.ruoyi.system.mapper.PhysicalStudentMapper;
import com.ruoyi.system.mapper.PositionStudentMapper;
import com.ruoyi.system.mapper.QualificationStudentMapper;
import com.ruoyi.system.service.IDashboardService;

/**
 * 仪表盘统计数据服务实现
 * 
 * @author ruoyi
 */
@Service
public class DashboardServiceImpl implements IDashboardService
{
    @Autowired
    private QualificationStudentMapper qualificationStudentMapper;

    @Autowired
    private PhysicalStudentMapper physicalStudentMapper;

    @Autowired
    private MedicalStudentMapper medicalStudentMapper;

    @Autowired
    private PositionStudentMapper positionStudentMapper;

    /**
     * 获取所有模块的汇总数据
     */
    @Override
    public Map<String, Object> getSummaryData()
    {
        Map<String, Object> data = new HashMap<>();
        
        // 获取各模块总人数
        int qualificationCount = qualificationStudentMapper.countTotalStudents();
        int physicalCount = physicalStudentMapper.countTotalStudents();
        int medicalCount = medicalStudentMapper.countTotalStudents();
        int positionCount = positionStudentMapper.countTotalStudents();
        
        data.put("qualificationCount", qualificationCount);
        data.put("physicalCount", physicalCount);
        data.put("medicalCount", medicalCount);
        data.put("positionCount", positionCount);
        
        // 添加流程数据统计
        // 计算通过率
        List<Map<String, Object>> qualificationStatusList = qualificationStudentMapper.countQualificationReviewStatus();
        List<Map<String, Object>> physicalStatusList = physicalStudentMapper.countPhysicalTestGrades();
        List<Map<String, Object>> medicalStatusList = medicalStudentMapper.countMedicalInterviewStatus();
        
        int qualificationPassCount = getPassCount(qualificationStatusList, "status", "通过");
        int physicalPassCount = getPassCount(physicalStatusList, "grade", "通过");
        int medicalPassCount = getPassCount(medicalStatusList, "status", "通过");
        
        // 计算各阶段通过率
        data.put("qualificationPassCount", qualificationPassCount);
        data.put("qualificationPassRate", qualificationCount > 0 ? Math.round((float) qualificationPassCount / qualificationCount * 100) : 0);
        
        data.put("physicalPassCount", physicalPassCount);
        data.put("physicalPassRate", physicalCount > 0 ? Math.round((float) physicalPassCount / physicalCount * 100) : 0);
        
        data.put("medicalPassCount", medicalPassCount);
        data.put("medicalPassRate", medicalCount > 0 ? Math.round((float) medicalPassCount / medicalCount * 100) : 0);
        
        // 计算最终选择职位的人数比例
        data.put("positionSelectRate", medicalPassCount > 0 ? Math.round((float) positionCount / medicalPassCount * 100) : 0);
        
        // 计算流程转化率（从资格审查到职位选择的完整流程转化率）
        data.put("overallConversionRate", qualificationCount > 0 ? Math.round((float) positionCount / qualificationCount * 100) : 0);
        
        return data;
    }
    
    /**
     * 从状态列表中获取通过的数量
     * 
     * @param statusList 状态列表
     * @param statusKey 状态键名
     * @param passValue 通过的值
     * @return 通过的数量
     */
    private int getPassCount(List<Map<String, Object>> statusList, String statusKey, String passValue) {
        if (statusList == null || statusList.isEmpty()) {
            return 0;
        }
        
        for (Map<String, Object> status : statusList) {
            if (passValue.equals(status.get(statusKey))) {
                Object countObj = status.get("count");
                if (countObj instanceof Integer) {
                    return (Integer) countObj;
                } else if (countObj instanceof Long) {
                    return ((Long) countObj).intValue();
                } else if (countObj != null) {
                    return Integer.parseInt(countObj.toString());
                }
            }
        }
        
        return 0;
    }

    /**
     * 获取资格审查模块的统计数据
     */
    @Override
    public Map<String, Object> getQualificationStats()
    {
        Map<String, Object> data = new HashMap<>();
        
        // 获取资格审查结果分布
        List<Map<String, Object>> reviewStatusList = qualificationStudentMapper.countQualificationReviewStatus();
        data.put("reviewStatusList", reviewStatusList);
        
        // 获取考生学历分布
        List<Map<String, Object>> educationList = qualificationStudentMapper.countEducationDistribution();
        data.put("educationList", educationList);
        
        return data;
    }

    /**
     * 获取体能测评模块的统计数据
     */
    @Override
    public Map<String, Object> getPhysicalStats()
    {
        Map<String, Object> data = new HashMap<>();
        
        // 获取体能测评成绩等级分布
        List<Map<String, Object>> gradesList = physicalStudentMapper.countPhysicalTestGrades();
        data.put("gradesList", gradesList);
        
        // 获取考生性别分布
        List<Map<String, Object>> genderList = physicalStudentMapper.countGenderDistribution();
        data.put("genderList", genderList);
        
        return data;
    }

    /**
     * 获取体检面试模块的统计数据
     */
    @Override
    public Map<String, Object> getMedicalStats()
    {
        Map<String, Object> data = new HashMap<>();
        
        // 获取体检面试结果分布
        List<Map<String, Object>> statusList = medicalStudentMapper.countMedicalInterviewStatus();
        data.put("statusList", statusList);
        
        // 获取面试成绩分布
        List<Map<String, Object>> scoreList = medicalStudentMapper.countInterviewScoreDistribution();
        data.put("scoreList", scoreList);
        
        // 获取通过率统计
        int totalCount = medicalStudentMapper.countTotalStudents();
        int medicalPassCount = medicalStudentMapper.countMedicalPassStudents();
        int interviewPassCount = medicalStudentMapper.countInterviewPassStudents();
        
        if (totalCount > 0) {
            data.put("medicalPassRate", Math.round((float) medicalPassCount / totalCount * 100));
            data.put("interviewPassRate", Math.round((float) interviewPassCount / totalCount * 100));
            data.put("totalPassRate", Math.round((float) interviewPassCount / totalCount * 100));
        } else {
            data.put("medicalPassRate", 0);
            data.put("interviewPassRate", 0);
            data.put("totalPassRate", 0);
        }
        
        data.put("medicalPassCount", medicalPassCount);
        data.put("interviewPassCount", interviewPassCount);
        data.put("totalPassCount", interviewPassCount);
        
        return data;
    }

    /**
     * 获取职位选择模块的统计数据
     */
    @Override
    public Map<String, Object> getPositionStats()
    {
        Map<String, Object> data = new HashMap<>();
        
        // 获取职位选择热度分布
        List<Map<String, Object>> positionList = positionStudentMapper.countPositionChoiceDistribution();
        data.put("positionList", positionList);
        
        return data;
    }
    
    /**
     * 初始化系统，清空所有模块数据
     */
    @Override
    public boolean initSystem()
    {
        try
        {
            // 清空所有模块表数据，使用SQL直接执行truncate命令
            qualificationStudentMapper.deleteAllQualificationStudents();
            physicalStudentMapper.deleteAllPhysicalStudents();
            medicalStudentMapper.deleteAllMedicalStudents();
            positionStudentMapper.deleteAllPositionStudents();
            return true;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return false;
        }
    }
} 